package com.aaa.qy127.controller;

import com.aaa.qy127.entity.Staff;
import com.aaa.qy127.service.StaffService;
import com.aaa.qy127.util.CodeAndMsg;
import com.aaa.qy127.util.Result;
import com.aaa.qy127.util.UploadUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/staff")
@RestController
public class StaffController {

    @Resource(name = "StaffServiceImpl")
    private StaffService staffService;

    /**
     * 登录页面
     * @param staffId
     * @param password
     * @param request
     * @return
     */
    @RequestMapping("/login")
    public CodeAndMsg login(String staffId, String password, HttpServletRequest request){

        //System.out.println("StaffController======"+staffId+"----"+password);

        CodeAndMsg codeAndMsg = staffService.getCodeAndMsg(staffId, password,request);

        //System.out.println("StaffController======"+codeAndMsg.getMsg()+"-----------"+codeAndMsg.getCode());

        return codeAndMsg;
    }

    /**
     * 查询 所有信息 业务的转发
     * @param staff
     * @return
     */
    @RequestMapping("/selectStaff")
    public Result selectStaff(Staff staff){
        Result result = staffService.selStaff(staff);
        return result;
    }

    /**
     * 修改状态 的业务的 转发
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/updateStatus")
    public  String updateStatus(Integer id,Integer status){
        String s = staffService.updStatus(id, status);
        return s;
    }

    /**
     * 修改 或新增 staff 的信息 的业务 的转发
     * @param staff
     * @return
     */
    @RequestMapping("/updateAndInsertStaff")
    public  CodeAndMsg updateAndInsertStaff(Staff staff){
        CodeAndMsg codeAndMsg = staffService.updAndInsStaff(staff);
        return codeAndMsg;
    }

    /**
     * 获得 staffId 最大值的 业务 的转发
     * @return
     */
    @RequestMapping("/maxStaffId")
    public  int maxStaffId(){
        int staffId = staffService.getStaffId();
        return staffId;
    }

    /**
     * 头像上传
     * @param file
     * @return
     */
    @RequestMapping("/upload")
    public Map upload(MultipartFile file){
        String url = UploadUtil.upload(file);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("url",url);
        return map;
    }

    /**
     * 修改密码
     * @param password
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/uppassword")


    public Map uppassword(String password, HttpServletRequest request, HttpServletResponse response)  {

        Staff staff = (Staff)request.getSession().getAttribute("staff");
        int i = staffService.updatePassword(staff.getStaffId(),password);
        Map<Object, Object> map = new HashMap<>();
        if(i>0){
            map.put("code","200");
            map.put("msg","修改密码成功");
        }else {
            map.put("code","300");
            map.put("msg","操作失败");
        }
        request.getSession().removeAttribute("staff");

       /* try {
            response.sendRedirect("/login.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        return map;
    }

    /**
     * 获取当前登录人的信息
     * @param request
     * @return
     */
    @RequestMapping("/getNowLoginStaffInformation")
    public List<Map<String,Object>> getNowLoginStaffInformation(HttpServletRequest request){
        return staffService.getNowLoginStaffInformation(request);
    }

    /**
     *
     * @param staff
     * @param request
     * @return
     */
    @RequestMapping("/updateStaffPhoneAndAddress")
    public int updateStaffPhoneAndAddress(Staff staff, HttpServletRequest request) {
        return staffService.updateStaffPhoneAndAddress(staff,request);
    }

    /**
     * 头像上传
     * @param file
     * @return
     */
    @RequestMapping("/imgUpload")
    public Map imgUpload(HttpServletRequest request,MultipartFile file){
        return  staffService.imgUpload(request,file);
    }
}
